package com.oxy.common.util;

import java.io.UnsupportedEncodingException;

public class FileUtil {
	public static String getClassPathAbsolute(){
		String classpath = FileUtil.class.getClassLoader().getResource("").getPath().substring(1);
		return getSafePath(classpath);
	}
	
	public static String getSafePath(String path) {
		String ret = path;
		try {
			if(path == null) {
				ret = "";
			}
			else {
				// 1. trim path
				// 2. replace all \ to / in case of any char escape
				// 3. remove the last slashes to avoid any unnecessary problem
				// e.g. "Hello, ${name}".replaceFirst("\\$\\{name\\}", "Edward\\"), it's throwing ArrayIndexOutOfBoundsException
				ret = path.trim().replace("\\", "/").replaceAll("/*$", "").replaceAll("//", "/");
			}
			
			ret = java.net.URLDecoder.decode(ret, "utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return ret;
	}
}
